package word;

import java.util.Random;

public class demo1 {
    public static int count1 = 0;
    public static int count2 = 0;
    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        //1.生成随机数
        int[] arr = new int[10000000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100)+1;
        }
        long startTime = System.currentTimeMillis();
        //2.求和
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10000000; i=i+2) {
                count1+=arr[i];
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 1; i < 10000000; i+=2) {
                count2+=arr[i];
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("总和="+count1+count1);
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);
    }
}
